package com.example.cargetter.utils;

import com.example.cargetter.utils.MqttDataUtils;
import com.example.cargetter.pojo.CarData;
import com.example.cargetter.pojo.WarnFlag;
import org.locationtech.proj4j.BasicCoordinateTransform;
import org.locationtech.proj4j.CRSFactory;
import org.locationtech.proj4j.CoordinateReferenceSystem;
import org.locationtech.proj4j.ProjCoordinate;

import java.util.HashMap;

public class LightUtil {
    public static void main(String[] args) {
        //获取主车经纬度
        CarData carData = CarData.getInstance(0,0,0,0);


//        double ego_lat1 = -74.0060;  // 主车纬度
//        double ego_lon1 = 40.7128;  // 主车经度

        double ego_lat1 = carData.getLat();  // 主车纬度
        double ego_lon1 = carData.getLon();  // 主车经度

        double lit_lat = ego_lat1+1;  // 红绿灯纬度
//        double lit_lat = -74.0070;  // 红绿灯纬度
//        double lit_lon = 40.7128;  //  红绿灯经度
        double lit_lon = ego_lon1+1;  //  红绿灯经度
        double alt = 0;  // 海拔高度

        String lit_stateString = MqttDataUtils.getInstance().get("lighttype");
        String lit_timeString = MqttDataUtils.getInstance().get("second");
        double lit_state = Double.parseDouble(lit_stateString);
        double lit_time = Double.parseDouble(lit_timeString);

        //交通灯状, 1代表黄灯，2代表红灯，3代表绿灯
//        double lit_state=0, lit_time=27;//状态，剩余时间

        //获取车速
        double ego_speed=carData.getSpeed();//主车速度
//        double ego_speed=30;//主车速度

        //主车的XY坐标
        double x1=getX(ego_lon1,ego_lat1,alt);
        double y1=getY(ego_lon1,ego_lat1,alt);

        //红绿灯的XY坐标
        double x2=getX(lit_lon,lit_lat,alt);
        double y2=getY(lit_lon,lit_lat,alt);

        //纵向偏移量
        double longitudinal_offset = Math.abs(x1 - x2);
        //横向偏移量
        double lateral_offset = Math.abs(y1 - y2);

        //主车距离红绿灯的距离
        double distance = Math.sqrt(Math.pow(longitudinal_offset, 2) + Math.pow(lateral_offset, 2));


        //判断在红灯或者黄灯的情况下，距离停车线的距离
        //0代表绿灯减速，1代表红灯停车
        if ( lit_state == 2 || lit_state == 1 ) {
            if(distance<40 && ego_speed!=0){
                //提醒红灯停车
                WarnFlag warnFlag = WarnFlag.getInstance();
                warnFlag.setFlag(2);
            }
        }
        else {
            //提醒在通过红绿灯时减速慢行
            WarnFlag warnFlag = WarnFlag.getInstance();
            warnFlag.setFlag(0);
        }

    }

    //获得x坐标
    public static double getX(double lon,double lat,double alt){
        // 创建投影对象
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem crsLatLon = crsFactory.createFromName("EPSG:4326");
        CoordinateReferenceSystem crsCartesian = crsFactory.createFromName("EPSG:3857");
        BasicCoordinateTransform transform = new BasicCoordinateTransform(crsLatLon, crsCartesian);

        // 经纬度转换为空间直角坐标系坐标
        ProjCoordinate source = new ProjCoordinate(lon, lat, alt);
        ProjCoordinate target = new ProjCoordinate();
        transform.transform(source, target);

        // 输出结果
        double x = target.x;  // x坐标
        return x;
    }
    //获得Y坐标
    public static double getY(double lon,double lat,double alt){
        // 创建投影对象
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem crsLatLon = crsFactory.createFromName("EPSG:4326");
        CoordinateReferenceSystem crsCartesian = crsFactory.createFromName("EPSG:3857");
        BasicCoordinateTransform transform = new BasicCoordinateTransform(crsLatLon, crsCartesian);

        // 经纬度转换为空间直角坐标系坐标
        ProjCoordinate source = new ProjCoordinate(lon, lat, alt);
        ProjCoordinate target = new ProjCoordinate();
        transform.transform(source, target);

        // 输出结果
        double y = target.y;  // y坐标
        return y;
    }

}
